package ysq.xyg.shop.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.shop.model.po.ShopOrder;
import ysq.xyg.shop.model.vo.ShopOrderCouponExportVo;
import ysq.xyg.shop.model.vo.ShopOrderCouponVo;
import ysq.xyg.shop.model.vo.ShopOrderVo;
import ysq.xyg.shop.service.ShopOrderCouponService;
import ysq.xyg.shop.service.ShopOrderService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商城模块-订单生成表controller层
 *
 * @author congcong tao
 * @date 2019-5-29 16:41:51
 */
@RestController
public class ShopOrderController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(ShopOrderController.class);

    @Resource
    private ShopOrderCouponService shopOrderCouponService;
    @Resource
    private ShopOrderService shopOrderService;

    private ShopOrder shopOrder;

    /**
     * 分页查询
     *
     * @param shopOrderVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author congcong tao
     * @date 2019-5-29 16:41:51
     */
    @BaseLog(module = "商城模块",className = "订单生成表",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopOrder/page",method = RequestMethod.POST)
    public ReturnEntity page(ShopOrderVo shopOrderVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = shopOrderService.page(shopOrderVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-5-29 16:41:51
     */
    @RequestMapping(value = "/shopOrder/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/shop/shopOrderUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            shopOrder = shopOrderService.selectByPrimaryKey(id);
            view.addObject("shopOrder", shopOrder);
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-5-29 16:41:51
     */
    @BaseLog(module = "商城模块",className = "订单生成表",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopOrder/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return shopOrderService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param shopOrderVo Vo对象
     * @author congcong tao
     * @date 2019-5-29 16:41:51
     */
    @BaseLog(module = "商城模块",className = "订单生成表",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopOrder/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,ShopOrderVo shopOrderVo){
        return shopOrderService.addOrUpdate(shopOrderVo,acceptCurrentUser(request));
    }

    /**
     * 导出
     * @param response
     * @param shopOrderCouponVo
     */
    @BaseLog(module = "商城模块",className = "订单表",methods = "导出",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping("/shopOrder/export")
    public void export(HttpServletResponse response, ShopOrderCouponVo shopOrderCouponVo){
        // 根据用户
        List<ShopOrderCouponExportVo> shopOrderCouponList = shopOrderCouponService.selectGroupByMember(shopOrderCouponVo);
        String nowDate = DateUtil.formatDate(DateUtil.date());
        // 导出操作
        EasyPoiUtil.exportExcel(shopOrderCouponList, nowDate + "订单展示", "订单", ShopOrderCouponExportVo.class, nowDate + "订单展示.xls", response);
    }
}